package com.yangluyao.test.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author YLY
 * @ClassName BlockingQueueDemo
 * @Date 2020/5/7
 * @Version 1.0.2
 */
public class ArrayBlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer("A",2, TimeUnit.SECONDS);
        blockingQueue.offer("A",2, TimeUnit.SECONDS);
        blockingQueue.offer("A",2, TimeUnit.SECONDS);
        System.out.println("#############################");
        blockingQueue.offer("A",2, TimeUnit.SECONDS);
    }

    /**
     * 当阻塞队列满时，再往队列里面add插入元素会抛出**IllegalStateException: Queue full**异常
     */
    private static void ArrayBlockingQueueAdd(){
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.add("1");
        blockingQueue.add("1");
        blockingQueue.add("1");
        blockingQueue.add("1");
    }

    /**
     * 当阻塞队列空时，再往队列里remove移除元素会抛出**NoSuchElementException**异常
     */
    private static void ArrayBlockingQueueAddAndRemove(){
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.add("a");
        blockingQueue.add("b");
        blockingQueue.add("c");
        blockingQueue.element();//检查队列是否为空，若不为空，则返回队列首部元素
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
    }

    /**
     * 使用offer()添加元素，若超出队列长度则返回false。
     */
    private static void ArrayBlockingQueueOffer(){
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("A"));
        System.out.println(blockingQueue.offer("A"));
        System.out.println(blockingQueue.offer("A"));
        System.out.println(blockingQueue.offer("A"));
    }

    /**
     * 插入成功返回true  插入失败返回false，peek()如果队列有值，返回队列首部元素，
     * 如果没有返回null,poll()取出队列，如果队列空时返回null.
     */
    private static void ArrayBlockingQueueOfferAndPoll(){
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("A"));
        System.out.println(blockingQueue.offer("B"));
        System.out.println(blockingQueue.offer("C"));
        System.out.println(blockingQueue.offer("D"));
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

    /**
     * put,当队列满时，会发生阻塞，程序会一直等待，take当队列空时会发生阻塞，程序会一直等待。
     */
    private static void ArrayBlockingQueuePut() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("A");
        blockingQueue.put("A");
        blockingQueue.put("A");
        System.out.println("##################");
        blockingQueue.put("A");
        blockingQueue.take();
        blockingQueue.take();
        blockingQueue.take();
    }
}
